class ParamsTypeError(Exception):
    def __init__(self, ErrorInfo):
        super().__init__(self)  # 初始化父类
        self.errorinfo = ErrorInfo

    def __str__(self):
        return self.errorinfo


# 通过匿名函数和常规函数根据键age来排序
def work_one(data_list):
    print('根据键age来排序：')
    # 正序
    data_list.sort(key=lambda x: x['age'])
    print(data_list)
    # 倒序
    data_list.sort(key=sort_by_lambda, reverse=True)
    print(data_list)
    print('----------')


def sort_by_lambda(x):
    return x['age']


# map函数将上述同学的age+1
def map_test(data_list):
    print('age+1结果集：')
    print("原始数据：{}".format(data_list))
    _m = map(lambda x: x['age'] + 1, data_list)
    age_list = list(_m)
    for i, row in enumerate(data_list):
        row['age'] = age_list[i]
    print("结果数据：{}".format(data_list))
    print('----------')


# filter函数过滤掉上述同学中grade是A
def filter_test(data_list):
    dl = filter(lambda x: x['grade'] != 'A', data_list)
    print('过滤 grade为 A的结果：\n{}'.format(list(dl)))
    print('----------')


# 递归来重构斐波那契函数
def func_fib(n):
    try:
        n = int(n)
    except:
        raise ParamsTypeError('n must be int')
    if n <= 1:
            return n
        else:
            return func_fib(n - 1) + func_fib(n - 2)

if __name__ == '__main__':
    classes = [
        {"name": "abyss", "age": 25, "grade": "A"},
        {"name": "kin", "age": 23, "grade": "B"},
        {"name": "tian", "age": 24, "grade": "B"},
        {"name": "hun", "age": 26, "grade": "A"},
        {"name": "exe", "age": 21, "grade": "A"}
    ]

    work_one(classes)

    map_test(classes)

    filter_test(classes)

    fib_list = [i for i in map(func_fib, range(10))]
    print(fib_list)
